#!/bin/bash -ex

# this script runs as root under docker

OS_TYPE="$1"
OS_VERSION="$2"

# build and install
yum install -y rpm-build make yum-utils gcc binutils util-linux-ng which
yum install -y libseccomp-devel e2fsprogs cryptsetup
yum install -y epel-release
yum install -y golang

# switch to an unprivileged user with sudo privileges
yum install -y sudo
# We need Git existing here prior to the run
yum install -y git
useradd -u 1000 --create-home -s /bin/bash testuser
echo "Defaults:testuser env_keep=DOCKER_HOST" >>/etc/sudoers
echo "testuser ALL=(ALL) NOPASSWD: ALL" >>/etc/sudoers
chown -R testuser .

SINGULARITY_DOCKER_USERNAME=$E2E_DOCKER_USERNAME \
SINGULARITY_DOCKER_PASSWORD=$E2E_DOCKER_PASSWORD \
su testuser -c '
  set -x
  set -e
  ./mconfig
  sudo yum-builddep -y singularity.spec
  make -C builddir rpm
  sudo yum install -y $HOME/rpmbuild/RPMS/*/*.rpm
  BLD=`echo $HOME/rpmbuild/BUILD/singularity-*`
  export GOPATH=$BLD/gopath
  PATH=$GOPATH/bin:$PATH

  cd $GOPATH/src/github.com/sylabs/singularity
  make -C builddir action-test
'
